const { defineConfig, globalIgnores } = require('eslint/config');
const js = require('@eslint/js');
const { FlatCompat } = require('@eslint/eslintrc');
const globals = require('globals');
const compat = new FlatCompat({
  baseDirectory: __dirname,
  recommendedConfig: js.configs.recommended,
  allConfig: js.configs.all,
});

module.exports = defineConfig([
  {
    extends: compat.extends('eslint:recommended'),
    languageOptions: {
      ecmaVersion: 2022,
      sourceType: 'script', // ← 关键：CommonJS 不是 ESM
      globals: {
        ...globals.node, // 提供 __dirname、require 等全局变量
        ...globals.browser,
      },
    },
    rules: {
      indent: ['error', 2],
      quotes: ['error', 'single'],
      semi: ['error', 'always'],
      // 关键修复：es5 → 对象
      'comma-dangle': [
        'error',
        {
          arrays: 'always-multiline',
          objects: 'always-multiline',
          imports: 'always-multiline',
          exports: 'always-multiline',
          functions: 'never',
        },
      ],
      'eol-last': ['error', 'always'],
      'no-trailing-spaces': 'error',
      'object-curly-spacing': ['error', 'always'],
      'array-bracket-spacing': ['error', 'never'],
      'key-spacing': ['error', { beforeColon: false, afterColon: true }],
      'keyword-spacing': ['error', { before: true, after: true }],
      'space-before-blocks': 'error',
      'space-infix-ops': 'error',
    },
  },
  {
    files: ['src/**/*.js'],
    languageOptions: {
      ecmaVersion: 'latest',
      sourceType: 'module',        // ← 关键
      globals: {
        ...globals.browser,
        $: 'readonly',
        themeConfig: 'readonly',
        moi: 'readonly',
        pako: 'readonly',
        storeConfig: 'readonly',
        shopUrl: 'readonly',
        oemcart_page_ca: 'readonly',
        formatMoney: 'readonly',
        oemcart_currency: 'readonly',
        oemcart_currency_rate: 'readonly',
        lang: 'readonly',
        engine: 'readonly',
        callback_share_product: 'readonly',
        callback_add_to_cart: 'readonly',
        miniCart: 'readonly',
        showMiniCart: 'readonly',
        oemcart_user_id: 'readonly',
        callback_add_to_wishlist: 'readonly',
        createAjaxLoading: 'readonly',
        removeAjaxLoading: 'readonly',
        _GET_C_SETTING_: 'readonly',
        order_price_detail: 'readonly',
        currencyExchange: 'readonly',
      },
    },
    rules: {
      ...js.configs.recommended.rules,
      'eol-last': ['error', 'always'],
      'no-trailing-spaces': 'error',
      'object-curly-spacing': ['error', 'always'],
      'array-bracket-spacing': ['error', 'never'],
      'key-spacing': ['error', { beforeColon: false, afterColon: true }],
      'keyword-spacing': ['error', { before: true, after: true }],
      'space-before-blocks': 'error',
      'space-infix-ops': 'error',
    },
  },
  globalIgnores(['**/node_modules/', '**/assets/*.{js,css,scss}']),
]);
